//
//  HomePlatformCell.swift
//  d11
//
//  Created by peale_pro on 2019/6/14.
//  Copyright © 2019年 Tech_001. All rights reserved.
//

import UIKit
typealias PlatformSeltBlock = (_ dict: [String:Any],_ index:Int) -> Void
class HomePlatformCell: UITableViewCell,UICollectionViewDelegate,UICollectionViewDataSource {
    var arrayList : [[String:Any]]? //数据列表
    var type : Int = 0
    var selectIndex = 0
    var selectDict : [String:Any]?
    var leftBtn : UIButton?
    var rightBtn :UIButton?
    var platformSeltBlock: PlatformSeltBlock?
    func callClickBlock(_ block: @escaping PlatformSeltBlock) {
        platformSeltBlock = block
    }
    lazy var collectionView:UICollectionView = {
        let layout = UICollectionViewFlowLayout.init()
//        if kScreenWidth<=320{
//            layout.itemSize = CGSize(width: (kScreenWidth-13-80)/4, height: ((kScreenWidth-13-80)/4)+45)
//            layout.sectionInset = UIEdgeInsets.init(top: 20, left: 10, bottom: 0, right: 10)
//            layout.minimumInteritemSpacing = 20
//        }else{
        if kScreenWidth<=320{
             layout.itemSize = CGSize(width: (kScreenWidth-57)/4, height: 85)
        }else{
             layout.itemSize = CGSize(width: (kScreenWidth-57)/4, height: 85)
        }
            layout.sectionInset = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
            layout.minimumInteritemSpacing = 0
      //  }
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .horizontal
       // layout.headerReferenceSize = CGSize.init(width: (kScreenWidth-20)/4, height: 87.5)
        // 设置分区头视图和尾视图宽高
        //    layout.headerReferenceSize = CGSize.init(width: kScreenWidth, height: 80)
        //    layout.footerReferenceSize = CGSize.init(width: kScreenWidth, height: 80)
        
        let collView = UICollectionView.init(frame: CGRect(x:0, y:0, width:(kScreenWidth-57), height:85), collectionViewLayout: layout)
        
        if collView.responds(to:#selector(setter: UIView.layoutMargins)) {
            collView.layoutMargins = .zero;
        }
        
        collView.autoresizingMask = UIView.AutoresizingMask.flexibleWidth.union(UIView.AutoresizingMask.flexibleHeight)
        collView.register(HomePlatformItemCell.self, forCellWithReuseIdentifier: "HomePlatformItemCell")
//        collView.register(HelperListHeadView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "HelperListHeadView1")
        collView.showsVerticalScrollIndicator = false
        collView.showsHorizontalScrollIndicator = false
        collView.backgroundColor = UIColor.white
        collView.delegate = self
        collView.dataSource = self
       // collView.backgroundColor = UIColor.red
        return collView
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    func setUpUI() -> Void {
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        self.arrayList = []
        let platforms = GlobalInfo.platformInfoData
        self.arrayList =  platforms
        self.backgroundColor = UIColor.white
         self.contentView.backgroundColor = UIColor.white
        self.contentView.addSubview(self.collectionView)
        
        self.leftBtn = UIButton.init(type: .custom)
        self.leftBtn?.setImage(imageNamed("home_platform_left"), for: .normal)
        self.leftBtn?.backgroundColor = UIColor.clear
        self.leftBtn?.isHidden = true
        self.leftBtn?.addTarget(self, action: #selector(actionToScrollLeft), for: UIControl.Event.touchUpInside)
        self.contentView.addSubview(self.leftBtn!)
        self.leftBtn?.snp.makeConstraints{(make) in
            make.width.equalTo(17.5)
            make.height.equalTo(19)
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(5)
        }
       
        self.rightBtn = UIButton.init(type: .custom)
        self.rightBtn?.setImage(imageNamed("home_platform_right"), for: .normal)
        self.rightBtn?.addTarget(self, action: #selector(actionToScrollRight), for: UIControl.Event.touchUpInside)
           self.rightBtn?.backgroundColor = UIColor.clear
        self.contentView.addSubview(self.rightBtn!)
        self.rightBtn?.snp.makeConstraints{(make) in
            make.width.equalTo(17.5)
            make.height.equalTo(19)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-5)
        }
        self.collectionView.snp.makeConstraints{(make) in
            //make.right.equalTo((self.rightBtn?.snp.left)!).offset(-6)
            make.height.equalTo(85)
            make.width.equalTo((kScreenWidth-57))
            make.left.equalTo((self.leftBtn?.snp.right)!).offset(6)
        }
    }

    //滚动到左侧
    @objc func actionToScrollLeft() -> Void {
        self.collectionView.setContentOffset(CGPoint.init(x: 0, y: 0), animated: true)
    }
    
    //滚动到右侧
    @objc func actionToScrollRight() -> Void {
        self.collectionView.setContentOffset(CGPoint.init(x: self.collectionView.contentSize.width-(self.collectionView.frame.size.width+0), y: 0), animated: true)
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == self.collectionView {
            if (self.collectionView.contentOffset.x <= 0)
            {
                //左侧
                print("到达左侧")
                self.leftBtn?.isHidden = true
            }else{
                self.leftBtn?.isHidden = false
            }

            if (self.collectionView.contentSize.width - self.collectionView.contentOffset.x-(self.collectionView.frame.size.width+10) <=  0)
            {
                
                //右侧
                print("到达右侧\(self.collectionView.contentSize.width)---\(self.collectionView.contentOffset.x)--\(self.collectionView.contentSize.width - self.collectionView.contentOffset.x-self.collectionView.frame.size.width)")
                self.rightBtn?.isHidden = true
            }else{
                self.rightBtn?.isHidden = false
            }
        }
    }
    
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return arrayList?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize.init(width: 0, height: 0)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize.init(width: 0, height: 0)
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        return UICollectionReusableView.init()
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:HomePlatformItemCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomePlatformItemCell", for: indexPath) as! HomePlatformItemCell
        let dict:[String:Any] = self.arrayList?[indexPath.row] ?? [:]
        var isSelect = false
        if indexPath.row ==  self.selectIndex {
            isSelect = true
        }
        cell.setData(imgUrl: dict["f_imgNameIOS"] as? String ?? "", selImgUrl: dict["f_imgNameIOSed"] as? String ?? "", title: dict["f_name"] as? String ?? "", isSelect: isSelect)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.selectIndex = indexPath.row
        if platformSeltBlock != nil {
            platformSeltBlock!(self.arrayList?[self.selectIndex] ?? [:],self.selectIndex)
        }
        self.collectionView.reloadData()
    }
    
    func reload() -> Void {
        let platforms = GlobalInfo.platformInfoData
        self.arrayList =  platforms
        self.collectionView.reloadData()
    }
}
